package cn.com.blueInfo.bpm.template.repository.impl;

import cn.com.blueInfo.bpm.common.entity.BaseEntity;
import cn.com.blueInfo.bpm.common.enums.BaseDeleted;
import cn.com.blueInfo.bpm.common.repository.BaseRepository;
import cn.com.blueInfo.bpm.template.entity.View;
import cn.com.blueInfo.bpm.template.mapper.ViewMapper;
import cn.com.blueInfo.bpm.template.repository.ViewRepository;
import cn.com.blueInfo.bpm.template.request.ViewDTO;
import cn.com.blueInfo.core.entity.CurrentLoginUserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 流程图配置表 数据服务实现类
 * @author suxch
 * @since 2025-09-01 19:30:44
 */
@Log4j2
@Service
public class ViewRepositoryImpl
        extends ServiceImpl<ViewMapper, View>
        implements ViewRepository, BaseRepository<View, ViewDTO.QueryDTO> {

    @Autowired
    private CurrentLoginUserInfo currentLoginUserInfo;

    /**
     * 提供获取 CurrentLoginUserInfo 的方法（由实现类提供，避免接口中注入）
     * @return CurrentLoginUserInfo
     */
    @Override
    public CurrentLoginUserInfo getCurrentLoginUserInfo() {
        return currentLoginUserInfo;
    }

    /**
     * 新增流程图配置
     * @param view 流程图配置参数
     */
    @Override
    public Integer createView(View view) {
        return sqlExecuteResult(save(view));
    }

    /**
     * 批量新增流程图配置
     * @param viewList 流程图配置参数列表
     */
    @Override
    public Integer batchCreateView(List<View> viewList) {
        return sqlExecuteResult(saveBatch(viewList), viewList.size());
    }

    /**
     * 删除流程图配置
     * @param viewId 流程图配置ID
     */
    @Override
    public Integer deleteView(Long viewId) {
        View view = new View();
        view.setId(viewId);
        view.setDeleted(BaseDeleted.DELETED);
        return sqlExecuteResult(removeById(view));
    }

    /**
     * 批量删除流程图配置
     * @param viewIdList 流程图配置ID列表
     */
    @Override
    public Integer batchDeleteView(List<Long> viewIdList) {
        List<View> viewList = new ArrayList<>();
        for (Long viewId : viewIdList) {
            View view = new View();
            view.setId(viewId);
            view.setDeleted(BaseDeleted.DELETED);
            viewList.add(view);
        }
        return sqlExecuteResult(removeBatchByIds(viewList), viewList.size());
    }

    /**
     * 更新部分流程图配置
     * @param view 流程图配置参数
     */
    @Override
    public Integer updateViewPartial(View view) {
        validateUpdateId(view.getId());
        LambdaUpdateWrapper<View> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(View::getId, view.getId());
        return sqlExecuteResult(update(updateWrapper));
    }

    /**
     * 更新全部流程图配置
     * @param view 流程图配置参数
     */
    @Override
    public Integer updateView(View view) {
        validateUpdateId(view.getId());
        return sqlExecuteResult(updateById(view));
    }

    /**
     * 查询一条流程图配置
     * @param viewId 流程图配置ID
     */
    @Override
    public View getViewById(Long viewId) {
        LambdaQueryWrapper<View> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(View::getId, viewId);
        return getOne(queryWrapper);
    }

    /**
     * 分页查询流程图配置
     * @param viewQueryDTO 流程图配置参数
     */
    @Override
    public Page<View> getViewPage(ViewDTO.QueryDTO viewQueryDTO) {
        Page<View> viewPage = new Page<>(
                viewQueryDTO.getPage(), viewQueryDTO.getSize());
        LambdaQueryWrapper<View> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, viewQueryDTO);

        BaseDeleted deleted = viewQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeletePage(viewPage, queryWrapper);
        } else {
            return page(viewPage, queryWrapper);
        }
    }

    /**
     * 根据条件查询 流程图配置 数量
     * @param viewQueryDTO 流程图配置参数
     */
    @Override
    public Long getViewCount(ViewDTO.QueryDTO viewQueryDTO) {
        LambdaQueryWrapper<View> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, viewQueryDTO);

        BaseDeleted deleted = viewQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteCount(queryWrapper);
        } else {
            return count(queryWrapper);
        }
    }

    /**
     * 根据条件查询 流程图配置
     * @param viewQueryDTO 流程图配置参数
     */
    @Override
    public List<View> getViewList(ViewDTO.QueryDTO viewQueryDTO) {
        LambdaQueryWrapper<View> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, viewQueryDTO);

        BaseDeleted deleted = viewQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteList(queryWrapper);
        } else {
            return list(queryWrapper);
        }
    }

    private void queryWrapper(LambdaQueryWrapper<View> queryWrapper, ViewDTO.QueryDTO viewQueryDTO) {

        Long processId = viewQueryDTO.getProcessId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(processId), View::getProcessId, processId);

        String nodes = viewQueryDTO.getNodes();
        queryWrapper.eq(StringUtils.isNotBlank(nodes), View::getNodes, nodes);

        String edges = viewQueryDTO.getEdges();
        queryWrapper.eq(StringUtils.isNotBlank(edges), View::getEdges, edges);

        String viewConfig = viewQueryDTO.getViewConfig();
        queryWrapper.eq(StringUtils.isNotBlank(viewConfig), View::getViewConfig, viewConfig);

        addQueryWrapper(queryWrapper, viewQueryDTO, getCommonColumn());

    }

    private Map<String, SFunction<View, ?>> getCommonColumn() {
        return BaseEntity.getCommonColumn(
                View::getOrderNumber,
                View::getTenantId,
                View::getApplicationId,
                View::getStatus,
                View::getDeleted,
                View::getDeleteUserId,
                View::getDeleteTime,
                View::getRemark,
                View::getCreateUserId,
                View::getCreateTime,
                View::getUpdateUserId,
                View::getUpdateTime,
                View::getExtend1,
                View::getExtend2,
                View::getExtend3);
    }

}
